package com.fallsvc.re.networkprogramming.threadpool;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @auther falls_vc
 * description:
 * @date 2025/10/29  19:55
 */
public class MyThreadPool {
    private BlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<>();

    public void submit(Runnable runnable) throws InterruptedException {
        blockingQueue.put(runnable);
    }

    public MyThreadPool(int x) {
        for (int i = 0; i < x; i++) {
            Thread thread = new Thread(()->{
               while (true) {
                   try {
                       Runnable runnable = blockingQueue.take();
                       System.out.println(Thread.currentThread());
                       runnable.run();
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
            });
            thread.start();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyThreadPool myThreadPool = new MyThreadPool(2);
        for (int i = 0; i < 10; i++) {
            myThreadPool.submit(()-> System.out.println("hh"));
        }

    }
}
